<!DOCTYPE html>
<html lang="en"  xmlns:th="http://www.thymeleaf.org">

<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>规格管理</title>
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <link rel="stylesheet" href="../plugins/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/AdminLTE.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/skins/_all-skins.min.css">
    <link rel="stylesheet" href="../css/style.css">
	<script src="../plugins/jQuery/jquery-2.2.3.min.js"></script>
    <script src="../plugins/bootstrap/js/bootstrap.min.js"></script>
	<script>
		$(function () {
			getData();
		})
		function deleteAll(){
			var ids = [];
			var rows = $("input:checked");
			$(rows).each(function (i,e) {
				ids.push(e.value);
			})
			if (ids.length<=0){
				alert("请选择需要删除的内容");
				return ;
			}
			$.ajax({
				url:"/specification/deleteAll",
				type:"post",
				data:{ids:ids},
				dataType:"json",
				success:function (result) {
					alert(result.message);
					getSearch();
				},
				error:function () {
					alert("删除失败");
				}
			})
		}
		function toUpdate(obj) {
			$.ajax({
				url:"/specification/toUpdate",
				type:"post",
				data:{id:obj},
				dataType:"json",
				async:false,
				success:function (result) {
					var t = result.data.t;
					console.log(t);
					var list = result.data.list;
					$("#udpateSpecName").val(t.specName);
					$("#id").val(t.id);
					var content = '<thead>\n' +
							'\t\t\t\t\t  <tr>\n' +
							'\n' +
							'\n' +
							'\t\t\t\t\t\t  <th class="sorting">规格选项</th>\n' +
							'\t\t\t\t\t\t  <th class="sorting">排序</th>\n' +
							'\t\t\t\t\t\t  <th class="sorting">操作</th>\n' +
							'\t\t\t\t\t  </thead>\n' +
							'\t\t\t\t\t  <tbody id="updateTable">';
					for (var i = 0; i < list.length; i++) {
						content += '<tr>\n' +
								'\n' +
								'\t\t\t\t\t\t  <td>\n' +
								'\t\t\t\t\t\t\t  <input value="'+list[i].optionName+'"  class="form-control" placeholder="规格选项">\n' +
								'\t\t\t\t\t\t  </td>\n' +
								'\t\t\t\t\t\t  <td>\n' +
								'\t\t\t\t\t\t\t  <input value="'+list[i].orders+'"  class="form-control" placeholder="排序">\n' +
								'\t\t\t\t\t\t  </td>\n' +
								'\t\t\t\t\t\t  <td>\n' +
								'\t\t\t\t\t\t\t  <button type="button"  onclick="dalete(this)" class="btn btn-default" title="删除" ><i class="fa fa-trash-o"></i> 删除</button>\n' +
								'\t\t\t\t\t\t  </td>\n' +
								'\t\t\t\t\t  </tr>';
					}
					$("#updateSpecOption").html(content);
				},
				error:function (){
					msg("修改失败");
				}
			})
		}

		function getData(obj) {
			var sname = $("#sname").val();
			$.ajax({
				url:"/specification/getinfo",
				type: "post",
				data:{number:obj,sname:sname},
				dataType: "json",
				success:function (page) {
					var result = page.data.result;
					var b = page.data;
					var count = "";
					for (var i = 0; i < result.length; i++) {
						count+= '\t\t\t  <tr>                            <td><input  type="checkbox" name="box" th:value="'+result[i].id+'"></td>\n' +
								'\t\t\t\t                          <td>'+result[i].id+'</td>\n' +
								'\t\t\t\t\t\t\t\t\t      <td>'+result[i].specName+'</td>\n' +
								'\t\t                                  <td class="text-center">                                           \n' +
								'\t\t                                 \t  <button type="button" class="btn bg-olive btn-xs" data-toggle="modal" data-target="#updateModal" onclick="toUpdate('+result[i].id+')">修改</button>\n' +
								'\t\t                                  </td>\n' +
								'\t\t\t                          </tr>'
					}
					$("#specTable").html(count);

					//分页
					var pageinfo = "";
					pageinfo += '<li><a href="javascript:getData(1)">首页</a></li>'
					if (b.pageNum > 1) {
						pageinfo += '<li class="threeword"><a href="javascript:getData(' + (b.pageNum - 1) + ')">上一页</a></li>'
					}
					pageinfo += '<li class="threeword">第 ' + b.pageNum + ' 页</li>'
					if (b.pageNum < b.pages) {
						pageinfo += '<li class="threeword"><a href="javascript:getData(' + (b.pageNum + 1) + ')">下一页</a></li>'
					}
					pageinfo += '<li class="threeword"><a href="javascript:getData(' + (b.pages) + ')">末页</a></li>'
					$("#page").html(pageinfo);

				},
				error:function () {
					alert("查询失败");
				}
			});
		}
		function getSearch() {
			window.location.reload();
		}
	</script>
    
</head>

<body class="hold-transition skin-red sidebar-mini" >
  <!-- .box-body -->                
                    <div class="box-header with-border">
                        <h3 class="box-title">规格管理</h3>
                    </div>
                    <div class="box-body">
                        <!-- 数据表格 -->
                        <div class="table-box">

                            <!--工具栏-->
                            <div class="pull-left">
                                <div class="form-group form-inline">
                                    <div class="btn-group">
                                        <button type="button" class="btn btn-default" title="新建" data-toggle="modal" data-target="#editModal" ><i class="fa fa-file-o"></i> 新建</button>
                                        <button type="button" class="btn btn-default" title="删除"  onclick="deleteAll()"><i class="fa fa-trash-o"></i> 删除</button>
                                        
                                        <button type="button" class="btn btn-default" title="刷新" onclick="window.location.reload();"><i class="fa fa-refresh"></i> 刷新</button>
                                    </div>
                                </div>
                            </div>
                            <div class="box-tools pull-right">
                                <div class="has-feedback">
							                    规格名称：<input  type="text" name="sname" id="sname">
									<button class="btn btn-default" onclick="getData()">查询</button>
                                </div>
                            </div>
                            <!--工具栏/-->
			                  <!--数据列表-->
			                  <table id="dataList" class="table table-bordered table-striped table-hover dataTable">
			                      <thead>
			                          <tr>
			                              <th class="" style="padding-right:0px">
			                                  <input id="selall" type="checkbox" class="icheckbox_square-blue">
			                              </th> 
										  <th class="sorting_asc">规格ID</th>
									      <th class="sorting">规格名称</th>									     												
					                      <th class="text-center">操作</th>
			                          </tr>
			                      </thead>
			                      <tbody id="specTable">
			                          <!--<tr th:each="s : ${slist}">
			                              <td><input  type="checkbox" name="box" th:value="${s.id}"></td>
				                          <td th:text="${s.id}"></td>
									      <td th:text="${s.specName}"></td>
		                                  <td class="text-center">                                           
		                                 	  <button type="button" class="btn bg-olive btn-xs" data-toggle="modal" data-target="#updateModal" th:onclick="'javascript:toUpdate('+${s.id}+')' ">修改</button>
		                                  </td>
			                          </tr>-->
			                      </tbody>
			                  </table>
							<div class="pageNum" id="page">
							</div>
			                  <!--数据列表/-->    
                        </div>
                        <!-- 数据表格 /--> 
                     </div>
                    <!-- /.box-body -->                    
	          		    
                                
<!-- 编辑新建窗口 -->
  <script>
	  function addinfo() {
	  	var options = [];
		  var specName = $("#addSpecName").val();
		  console.log(specName);
		  var table = $("#specOption");
		  var trs = $(table).find("tr");
		  for (var i = 1;i < trs.length;i++){
		  	var tds = $(trs[i]).find("td");
		  	var option = {"optionName":$(tds[0]).find("input").val(),"orders":$(tds[1]).find("input").val()};
		  	options.push(option);
		  }
		  var OptionString = JSON.stringify(options);
		  $.ajax({
			  url:"/specification/addinfo",
			  type:"post",
			  data:{specName:specName,OptionString:OptionString},
			  dataType:"json",
			  async:false,
			  success:function (result) {
				  getSearch();
				  alert(result.message);
			  },
			  error:function (){
				  alert("增加失败");
			  }
		  })
	  }
	  //修改后提交
	  function updateinfo() {
		  var options = [];
		  var specName = $("#udpateSpecName").val();
		  var id = $("#id").val();
		  console.log(specName);
		  var table = $("#updateSpecOption");
		  var trs = $(table).find("tr");
		  for (var i = 1;i < trs.length;i++){
			  var tds = $(trs[i]).find("td");
			  var option = {"optionName":$(tds[0]).find("input").val(),"orders":$(tds[1]).find("input").val()};
			  options.push(option);
		  }
		  var OptionString = JSON.stringify(options);
		  $.ajax({
			  url:"/specification/updateinfo",
			  type:"post",
			  data:{specName:specName,OptionString:OptionString,id:id},
			  dataType:"json",
			  async:false,
			  success:function (result) {
				  getSearch();
				  alert(result.message);
			  },
			  error:function (){
				  alert("修改失败");
			  }
		  })
	  }
  </script>
  <script type="text/javascript">
	  function addmake(){
		  var tab=document.getElementById("addTable");
		  var tr=document.createElement("tr");
		  tr.innerHTML='<td><input  class="form-control" placeholder="规格选项" name="option_name"></td><td><input  class="form-control" placeholder="排序" name="orders"></td><td><button type="button" class="btn btn-default" title="删除" onclick="dalete(this)" ><i class="fa fa-trash-o"></i> 删除</button></td>';
		  tab.appendChild(tr);
	  }

	  function updatemake(){
		  var tab=document.getElementById("updateTable");
		  var tr=document.createElement("tr");
		  tr.innerHTML='<td><input  class="form-control" placeholder="规格选项" name="option_name"></td><td><input  class="form-control" placeholder="排序" name="orders"></td><td><button type="button" class="btn btn-default" title="删除" onclick="dalete(this)" ><i class="fa fa-trash-o"></i> 删除</button></td>';
		  tab.appendChild(tr);
	  }

	  function dalete(obj){
		  var d =(obj.parentNode.parentNode);
		  d.parentNode.removeChild(d);
	  }
  </script>
<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog" >
	<div class="modal-content">
		<div class="modal-header">
			<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
			<h3 id="myModalLabel">规格编辑</h3>
		</div>
		<div class="modal-body">							
			
			<table class="table table-bordered table-striped"  width="800px">
		      	<tr>
		      		<td>规格名称</td>
		      		<td><input  class="form-control" placeholder="规格名称" id="addSpecName">  </td>
		      	</tr>
			 </table>				
			 
			 <!-- 规格选项 -->
			 <div class="btn-group">
                  <button type="button" class="btn btn-default" title="新建" onclick="addmake()"><i class="fa fa-file-o"></i> 新增规格选项</button>
                  
             </div>
			 
			 <table id="specOption" class="table table-bordered table-striped table-hover dataTable">
                    <thead>
                        <tr>
                          
					 
				      <th class="sorting">规格选项</th>
				      <th class="sorting">排序</th>																
                      <th class="sorting">操作</th>	
                    </thead>
                    <tbody  id="addTable">
                      <tr>
                           
				            <td>
				            	<input  class="form-control" placeholder="规格选项"> 
				            </td>
				            <td>
				            	<input  class="form-control" placeholder="排序"> 
				            </td>
							<td>
								<button type="button" class="btn btn-default" title="删除" onclick="delete(this)"><i class="fa fa-trash-o"></i> 删除</button>
							</td>
                      </tr>
                    </tbody>
			  </table> 
			
			
		</div>
		<div class="modal-footer">						
			<button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick="addinfo()">保存</button>
			<button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
		</div>
	  </div>
	</div>
</div>

<!--修改模态框-->
  <div class="modal fade" id="updateModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	  <div class="modal-dialog" >
		  <div class="modal-content">
			  <div class="modal-header">
				  <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
				  <h3 id="udpateModalLabel">规格编辑</h3>
			  </div>
			  <div class="modal-body">
				  <table class="table table-bordered table-striped"  width="800px">
					  <tr>
						  <td>规格名称</td>
						  <td><input  class="form-control" placeholder="规格名称" id="udpateSpecName">
						  <input  class="form-control" placeholder="规格名称" id="id"> </td>
					  </tr>
				  </table>

				  <!-- 规格选项 -->
				  <div class="btn-group">
					  <button type="button" class="btn btn-default" title="新建" onclick="updatemake()"><i class="fa fa-file-o"></i> 新增规格选项</button>

				  </div>

				  <table id="updateSpecOption" class="table table-bordered table-striped table-hover dataTable">
					  <thead>
					  <tr>
						  <th class="sorting">规格选项</th>
						  <th class="sorting">排序</th>
						  <th class="sorting">操作</th>
					  </thead>
					  <!--<tbody id="updateTable">
						&lt;!&ndash;  <tr>

							  <td>
								  <input  class="form-control" placeholder="规格选项">
							  </td>
							  <td>
								  <input  class="form-control" placeholder="排序">
							  </td>
							  <td>
								  <button type="button" class="btn btn-default" title="删除" onclick="delete(this)"><i class="fa fa-trash-o"></i> 删除</button>
							  </td>
						  </tr>&ndash;&gt;
					  </tbody>-->
				  </table>


			  </div>
			  <div class="modal-footer">
				  <button class="btn btn-success" data-dismiss="modal" aria-hidden="true" onclick="updateinfo()">保存</button>
				  <button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
			  </div>
		  </div>
	  </div>
  </div>

</body>

</html>